package com.logic.meeting.app.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.logic.meeting.app.R;
import com.logic.meeting.app.event.HomeRefreshEvent;
import com.logic.meeting.app.fusion.FusionPre;
import com.logic.meeting.app.service.HeartBeatService;
import com.logic.meeting.core.delegates.AtmosDelegate;
import com.logic.meeting.core.util.StringUtil;
import com.logic.meeting.core.util.storage.AtmosPreference;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class IndexBaseDelegate extends AtmosDelegate {
    @Override
    public Object setLayout() {
        return R.layout.delegate_index_base;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        if (!AtmosPreference.getCustomBooleanPre(FusionPre.IS_BIND)) {
            getSupportDelegate().loadRootFragment(R.id.delegate_container, new IndexMachineSetDelegate());
        } else {
            getSupportDelegate().loadRootFragment(R.id.delegate_container, new IndexMachineHomeDelegate());
            Intent serviceIntent = new Intent(getContext(), HeartBeatService.class);
            getProxyActivity().startService(serviceIntent);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetFinish(HomeRefreshEvent event) {
        if (event != null && event.getData() != null) {
            getSupportDelegate().loadRootFragment(R.id.delegate_container, new IndexMachineHomeDelegate());
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
